unit Utility;

interface

uses
  Classes, StrUtils, SysUtils, ADODB, DB;


const
  C_TransPrefix = 'Tab_S_Transaction';


function GetTransSQL(ADOConn: TADOConnection; BeginTime, EndTime: TDateTime): string;


implementation




{Result := (select * from XXXX) union (select * from XXXX)
 Result := '' none table
}
function GetTransSQL(ADOConn: TADOConnection; BeginTime, EndTime: TDateTime): string;
var
  FTableList: TStrings;
  TransName: string;
  ayear, aMonth, aday: Word;
begin
  Result := '';

  DecodeDate(BeginTime, ayear, aMonth, aday);
  BeginTime := EncodeDate(ayear, aMonth, 1);

  
  FTableList := TStringList.Create;
  try
    ADOConn.Connected := True;
    ADOConn.GetTableNames(FTableList, False);

    while BeginTime < EndTime + 1 do
    begin
      TransName := C_TransPrefix + FormatDateTime('yyyymm', BeginTime);
      if FTableList.IndexOf(TransName) >= 0 then
      begin

       if Result <> '' then
         Result := Result + ' union ';

       Result:=Result+'(Select * from ' + TransName + ')';
      end;
      BeginTime:=incmonth(BeginTime);
    end;

  finally
    FTableList.Free;
  end;

end;

end.
